package com.xbongbong.crm.perform.query.list.button;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.OpportunityFocusEntity;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.saas.enums.ListOptionEnum;
import com.xbongbong.saas.model.OpportunityFocusModel;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.elasticsearch.common.inject.Singleton;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * 机会的关注
 * @author 吴峰
 * @date 2021/12/20 14:47
 */
@Component
public class OpportunityFocus extends CustomerFocus {

    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private OpportunityFocusModel opportunityFocusModel;


    private boolean enable;

    private Collection<Long> list;



    public OpportunityFocus() {
    }

    public OpportunityFocus(boolean enable, Collection<Long> list) {
        this.enable = enable;
        this.list = list;
    }

    /**
     * 获取关注列表
     * @param corpid
     * @param userId
     * @param dataIdList
     * @return
     * @throws XbbException
     */
    private  OpportunityFocus getFocusList( String corpid, String userId, List<Long> dataIdList) throws XbbException {

        boolean isCusShare = companyConfigModel.hasBeenEnable(corpid, CompanyConfigEnum.SHARE_FOCUS_OPPORTUNITY.getAlias(), BasicConstant.IS_USE.toString());
        Set<Long> list = new HashSet<>();
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        if(!isCusShare) {
            map.put("userId", userId);
            map.put("groupByStr", " opportunity_id ");
        }
        map.put("opportunityIdIn", dataIdList);
        map.put("del", 0);
        map.put("focus", 1);
        List<OpportunityFocusEntity> customerFocusEntityList = opportunityFocusModel.findEntitys(map);
        for (OpportunityFocusEntity entity : customerFocusEntityList) {
            list.add(entity.getOpportunityId());
        }
        return new OpportunityFocus(true, list);
    }


    public OpportunityFocus setFocusField(String corpid, String userId, List<Long> dataIdList, List<FieldAttrEntity> list) throws XbbException {
        OpportunityFocus focus = getFocusList( corpid, userId, dataIdList);
        if (focus.isEnable()) {
            FieldAttrEntity fieldFocus = new FieldAttrEntity();
            fieldFocus.setAttr(ListOptionEnum.FOCUS.getAttr());
            fieldFocus.setAttrName(ListOptionEnum.FOCUS.getAttrName());
            fieldFocus.setFieldType(FieldTypeEnum.FOCUS.getType());
            fieldFocus.setDisableListSort(1);
            list.add(0, fieldFocus);
        }
        return focus;
    }



    @Override
    public boolean isEnable() {
        return enable;
    }

    @Override
    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    @Override
    public Collection<Long> getList() {
        return list;
    }

    @Override
    public void setList(Collection<Long> list) {
        this.list = list;
    }
}
